<?php

$loader->requireOnce('includes/RadConnector/RadAssignmentConnector.class.php');
$loader->requireOnce('includes/RadConnector/RadResultConnector.class.php');
/**
 * Controller of actions related to making radiology assignments
 *
 */
class C_RadAssignment extends Controller {
    private $logger ;

    function __construct($template_mod = "general") {
        parent::__construct($template_mod);
        $this->logger = LoggerManager::getLogger('core.controllers.C_RadAssignment');
        $this->template_mod = $template_mod;
        $this->view->path="radiology";
    }

    /**
     * Load panel to allow user creating assignments
     *
     * @param int $ticketId Ticket Id
     * @return array Result array of structure ('error'=>array, 'result'=>data)
     */
    public function actionLoadRadiologyAssignmentPanelAjax($ticketId = 0) {
        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL, 'mess'=>''), 'result'=>'');
        try {
        if (isset($_GET['ticketId'])) {
            $ticketId =   $_GET['ticketId'];
        }
        $this->logger->info("Start action LoadRadiologyAssignmentPanelAjax {ticketId=$ticketId}");
        $radTypeList   =   ORDataObject::factory('ParacliniqueConfig')->getRadiologyTypes();
        $this->assign('ticketId', $ticketId);
        $this->assign('radTypeList', $radTypeList);

        // Build radiology assignment list by connector
        $objConnector   =   new RadAssignmentConnector();
        $assignmentList =   $objConnector->loadRadiologyAssignmentList($ticketId, 'reloadRadPanel' . $ticketId);
        $this->assign('assignmentList', $assignmentList);
        $result['result']   =  $this->view->render('radiology_assignment_panel.html');
        }
        catch (HISFileLoaderException $e) {
            $result['error']['code'] = CoreErrorCode::LOAD_RAD_MODULE_FAIL;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::LOAD_RAD_MODULE_FAIL);
            $this->logger->error("Action LoadRadiologyAssignmentInputAjax fail. Error:" . $e->getMessage());
        }
        $this->logger->info("Finish action LoadRadiologyAssignmentPanelAjax {ticketId=$ticketId}");

        return json_encode($result);
    }

    /**
     * Load radiology assignment input page
     *
     * @return array Result array of structure ('error'=>array, 'result'=>data)
     */
    public function actionLoadAssignmentInputAjax() {
        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL, 'mess'=>''), 'result'=>'');
        $ticketId       =   $_GET['ticketId'];
        $enumRadType    =   $_GET['radType'];
        $this->logger->info("Start action LoadAssignmentInputAjax {ticketId=$ticketId, enumRadType=$enumRadType}");
        try {
            // Get assignment input page via connector
            $objConnector   =   new RadAssignmentConnector();
            $html           =   $objConnector->loadAssignmentInputDetails($enumRadType, $ticketId, "reloadRadPanel" . $ticketId);
            $result['result']   =  $html;
        }
        catch (Exception $e){
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $this->logger->error("Action LoadRadiologyAssignmentInputAjax fail. Error:" . $e->getMessage());
        }
        $this->logger->info("Finish action LoadAssignmentInputAjax {ticketId=$ticketId, enumRadType=$enumRadType}");

        return json_encode($result);
    }

    /**
     * Save a radiology assignment ticket
     *
     * @return array Result array of structure ('error'=>array, 'result'=>data)
     */
    public function actionSaveAssignmentTicketAjax() {
    	$result = array('error'=>array('code'=>CoreErrorCode::NORMAL, 'mess'=>''), 'result'=>'');
    	$this->logger->info('Start action saveAssignmentTicketAjax {ticketId='. $_POST['ticketId'] . '}');
    	try {
        $ticketId   =   $_POST['ticketId'];
        // Determine patient type
        $objTicket  =   Celini::newORDO('Ticket', $ticketId);
        $enumPatientType    =   $objTicket->getEnumPatientType();
        if ($enumPatientType == Utils::getEnumKey(EnumerationConstant::PATIENT_TYPE, EnumerationConstant::PATIENT_TYPE_INPATIENT)){
            // Initiate InPatientRadConnector
            $objRadConnector    =   new InPatientRadConnector();
        }
        else {
            // Inniate OutPatientRadConnector
            $objRadConnector    =   new OutPatientRadConnector();
        }

        // HIS: Do before create radiology assigment ticket
        $objRadConnector->doBeforeCreateAssignment();

        // RIS: Create assignment ticket
        $_POST['departmentId']  =   $this->_me->get_department_id();
        $objConnector   =   new RadAssignmentConnector();
        $radTicketId    =   $objConnector->saveAssignmentTicket($_POST);

        // HIS: Do after create assigment ticket
        $objRadConnector->doAfterCreateAssignment($ticketId, $radTicketId, date('Y-m-d H:i:s'));
    	}
    	catch (HISFileLoaderException $e) {
             $result['error']['code'] = CoreErrorCode::LOAD_RAD_MODULE_FAIL;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::LOAD_RAD_MODULE_FAIL);
            $this->logger->error("Action saveAssignmentTicketAjax fail. Error:" . $e->getMessage());
    	}
    	catch (Exception $e) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $this->logger->error("Action LoadRadiologyAssignmentInputAjax fail. Error:" . $e->getMessage());
    	}
        $this->logger->info('Finish action saveAssignmentTicketAjax {ticketId='. $_POST['ticketId'] . '}');

        return json_encode($result);
    }

    public function actionLoadRadResultAjax() {
    	$ticketId  =   $_GET['ticketId'];
    	$result = array(
    	   'error' =>  array(
    	                   'code'  =>  CoreErrorCode::NORMAL,
    	                   'mess'=>''),
            'result'=>''
    	);
        $this->logger->info("Start action LoadRadResultAjax {ticketId=$ticketId}");
        try {
            // Get assignment input page via connector
            $objConnector   =   new RadResultConnector();
            $html           =   $objConnector->loadRadResultView($ticketId);
            $result['result']   =  $html;
        }
        catch (Exception $e){
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $this->logger->error("Action LoadRadResultAjax fail. Error message:" . $e->getMessage());
        }
        $this->logger->info("Finish action LoadRadResultAjax {ticketId=$ticketId}");
        return json_encode($result);
    }
}
?>